package com.zznzzn.androidshixun0826;

import android.app.ActionBar;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class OpeningActivity extends AppCompatActivity {

    private Button mpass = null;
    private TextView mtitle, mprogress, mduration = null;
    private SeekBar mskb = null;
    private MediaPlayer mplayer = null;
    private Handler mhandler = null;
    private View.OnClickListener mbtnListener = null;
    private Timer mtimer;



    //自定义task
    class MyTimeTask extends TimerTask{

        private Handler hander;

        public MyTimeTask(Handler hander) {
            this.hander = hander;
        }

        @Override
        public void run() {
            this.hander.sendMessage(this.hander.obtainMessage());
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opening);
        getSupportActionBar().hide();

        mpass = findViewById(R.id.open_pass);
        mskb = findViewById(R.id.open_seekbar);
        mtitle = findViewById(R.id.open_title);
        mprogress = findViewById(R.id.open_progress);
        mduration = findViewById(R.id.open_duration);

        mhandler = new Handler(){//刷新界面
            @Override
            public void handleMessage(Message msg) {
                int now = mplayer.getCurrentPosition()/1000;
                String cur = String.format("%d:%02d",now/60,now%60);
                mprogress.setText(cur);
                mskb.setProgress(mplayer.getCurrentPosition());
            }
        };

        mskb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mplayer.seekTo(seekBar.getProgress());
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        mbtnListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.open_pass:
                        do_pass();
                        break;
                    default:
                        break;
                }
            }
        };
        mpass.setOnClickListener(mbtnListener);

        mtimer = new Timer();

        initPlayer();
        startPlayer();

    }

    private void do_pass() {
        mplayer.stop();
        mtimer.cancel();

        mplayer.release();
        mplayer = null;
        mtimer = null;

        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void startPlayer() {
        mplayer.start();
        mtimer.schedule(new MyTimeTask(mhandler),0,1000);
    }

    private void initPlayer() {
        mplayer = MediaPlayer.create(this ,R.raw.daerwen);
        int duration = mplayer.getDuration()/1000;
        String dur = String.format("%d:%02d",duration/60,duration%60);
        mduration.setText(dur);
        mprogress.setText("0:00");
        mskb.setMax(mplayer.getDuration());
//        mtimer.schedule(new MyTimeTask(mhandler),0,1000);


    }
}
